package triangleWorm;

import processing.core.*;

public class SimpleWorld implements IWorld{	
	PApplet p;

	int meshVertexNum = 30;
	PVector[][][] worldMesh = new PVector[meshVertexNum][meshVertexNum][meshVertexNum];
	int worldMeshSize = 2000;
	int meshVertexDist = worldMeshSize / meshVertexNum;
	
	int meshVertexSize = 2;
	
	public void init(PApplet processing) {
		p = processing;
		
		for(int x = 0; x < meshVertexNum; x ++) {
			for(int y = 0; y < meshVertexNum; y ++) {
				for(int z = 0; z < meshVertexNum; z ++) {
					
					float xPos = p.width / 2 - worldMeshSize / 2 + meshVertexDist * x;
					float yPos = p.height / 2 - worldMeshSize / 2 + meshVertexDist * y;
					float zPos =  - worldMeshSize / 2 + meshVertexDist * z;

					worldMesh[x][y][z] = new PVector(xPos, yPos, zPos);
				}				
			}
		}		
	}

	public void update() {
		// TODO Auto-generated method stub
		
	}

	public void draw() {

		p.background(0);
		
		/*
		//Bounding box
		p.pushMatrix();
			p.translate(worldMesh[0][0][0].x, worldMesh[0][0][0].y, worldMesh[0][0][0].z);
			//Front / back
			p.pushMatrix();
				p.fill(0, 255, 0, 50);
				p.rect(0,0,worldMeshSize, worldMeshSize);
				p.translate(0, 0, worldMeshSize);
				p.rect(0,0,worldMeshSize, worldMeshSize);
			p.popMatrix();
			//Top / bottom
			p.pushMatrix();
				p.rotateX(90f / 180f * PApplet.PI);
				p.fill(0, 0, 255, 100);
				p.rect(0,0,worldMeshSize, worldMeshSize);
				p.translate(0, 0, -worldMeshSize);
				p.rect(0,0,worldMeshSize, worldMeshSize);
			p.popMatrix();
			//Sides
			p.pushMatrix();
				p.rotateX(90f / 180f * PApplet.PI);
				p.rotateY(90f / 180f * PApplet.PI);
				p.fill(255, 0, 0, 100);
				p.rect(0,0,worldMeshSize, worldMeshSize);
				p.translate(0, 0, worldMeshSize);
				p.rect(0,0,worldMeshSize, worldMeshSize);
			p.popMatrix();
		p.popMatrix();	
		*/
			
		//Grid
		//Declare variables outside of loop
		float xPos;
		float yPos;
		float zPos;
		//System.out.println("WORLD");
		for(int x = 0; x < meshVertexNum; x ++) {
			for(int y = 0; y < meshVertexNum; y ++) {
				for(int z = 0; z < meshVertexNum; z ++) {
					
					xPos = worldMesh[x][y][z].x;
					yPos = worldMesh[x][y][z].y;
					zPos = worldMesh[x][y][z].z;					
					
					p.beginShape(PApplet.POINTS);
						p.stroke(255);
						p.fill(255);
						p.vertex(xPos, yPos, zPos);
					p.endShape();
				}				
			}
		}
		p.noStroke();
	}

	@Override
	public boolean checkCollisions(PVector[] objectPositions) {
		// TODO Auto-generated method stub
		return false;
	}

	public PVector getSize() {
		return new PVector(worldMeshSize, worldMeshSize, worldMeshSize);
	}

	public PVector getCenter() {
		return new PVector(p.width / 2f, p.height / 2f, 0);
	}

}
